<html>
  <body>
    
    <button onclick="copyCode()">点击复制</button>
  </body>

  <script>
    function copyText(text, callback) {
      // let input = document.createElement('input') input 无法实现换行
      let input = document.createElement("textarea");
      input.setAttribute("readonly", "readonly"); // 防止手机上弹出软键盘
      // input.setAttribute('value', text)
      input.innerHTML = text;
      document.body.appendChild(input);
      input.select();
      document.execCommand("copy");
      document.body.removeChild(input);
      if (callback) {
        callback(text);
      }
    }
    function copyCode() {
      let str = `SN:123\nMAC:7777\rHH:999`;
      this.copyText(str, () => {
        // alert('复制成功')
      });
    }
  </script>
</html>
